var phone = GetQueryString('phone'),
    userId = getItem('userId');
var twice = false;
var verifyCode;
//发送验证码
$('.code-button').click(function () {
    sendCode();
})

document.getElementById('verify_certain').addEventListener('tap',function () {
    $.ajax({
        //验证码是否正确接口
        url:'/imageVerifyC/verifyCode.json',
        dataType:'json',
        data:{
            mobile:phone,
            validateCode:$('#code_input').val(),
            companyCode:getItem('companyCode'),
        },
        type:'post',
        success:function (data) {
            if(data.code!=1){
                mui.toast(data.msg);
                return;
            }
            if(data.code==1){
                if(data.verifyCode){
                    $('.zhezhao').hide();
                    twice=true;
                    verifyCode = $('#code_input').val();
                    setItem('verifyCode',$('#code_input').val());
                    verify();
                }else{
                    mui.toast('验证码错误');
                    $('#code_input').val('');
                    var timestamp = (new Date()).valueOf();
                    $("#imgObj").attr("src","/imageVerifyC/validateCode.json?mobile="+phone+"&t="+timestamp);
                    return;
                }
            }
        }
    })
})


//关闭验证窗口
$('#close').click(function () {
    $('.zhezhao').hide();
})

function sendCode() {
    //验证该手机号是否发送过验证码
    $.ajax({
        url:'/imageVerifyC/ifCode.json',
        dataType:'json',
        data:{
            mobile:phone,
            companyCode:getItem('companyCode'),

        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                if(data.ifCode){
                    //如果为true，从本地获取图片验证码
                    verifyCode = getItem('verifyCode');
                    verify();
                }else{
                    //如果为false，需要输入
                    var timestamp = (new Date()).valueOf();
                    $("#imgObj").attr("src","/imageVerifyC/validateCode.json?mobile="+phone+"&t="+timestamp);
                    $('#code_input').val('');
                    $('.zhezhao').show();
                }
            }
            if(data.code==0){
                mui.toast(data.msg);
                return
            }
        }
    })
}

function verify() {
    $(".code-button").html("发送中…");
    $(".code-button").unbind();
    $.ajax({
        url: '/user/sendMobileCode_ValidateCode.json',
        data: {
            mobile: phone,
            platform: 'wap',
            validateCode:verifyCode,
            companyCode:getItem('companyCode'),
        },
        dataType: 'json',
        success: function (data) {
            if (data.code != 1) {
                mui.toast(data.msg);
                $(".code-button").html("重新发送");
                $(".code-button").bind('click', sendCode);
                return;
            }
            timeUpdate.init($(".code-button"), 60);

        }
    })
}

var timeUpdate = {
    node: null,//节点
    time: 60,//时间
    //主函数
    start: function () {
        if (this.time > 0) {
            $(this.node).html(this.time-- + 's后重发');
            var _this = this;
            setTimeout(function () {
                _this.start();
            }, 1000);
        } else {
            this.node.click(function () {
                sendCode();
            });
            this.node.html("重新发送");
        }
    },
    //初始化
    init: function (node, timeLimit) {
        this.node = node;
        this.time = timeLimit;
        this.start();
    }
};

//注册
$('#login-button').click(function () {
    if (!ifCyber()) {
        mui.toast('请检查网络');
    }
    $.ajax({
        url: '/user/checkCode_register.json',
        data: {
            mobile: phone,
            mobile_code: $('#phone').val(),
            platform: 'wap',
            id: userId,
            password: $('.password').val(),
            companyCode:getItem('companyCode'),
        },
        dataType: 'json',
        beforeSend:function(){
            $('<div class="loading2"><span class="mui-spinner"></span></div>').appendTo($('body'));
        },
        success: function (data) {
            $('.loading2').hide();
            if (data.code != 1) {
                mui.toast(data.msg);

            } else {
                setItem('token', data.datas.token);
                setItem('phone', phone);
                setItem('userId', data.datas.userId);
                window.location.href = 'index.html';

            }

        }
    })

})